package IHM;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class AfficheurImpl extends JFrame implements Afficheur {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<LEDImpl> listeLed;
	private MoletteImpl molette;
	private JButton start, stop, inc, dec;
	private JLabel tempoText, tempoValue;
	private int tempsParMesure=4;


	public AfficheurImpl(){
		listeLed = new ArrayList<LEDImpl>();
		LEDImpl led1 = new LEDImpl();
		LEDImpl led2 = new LEDImpl();
		listeLed.add(led1);
		listeLed.add(led2);
		
		molette = (MoletteImpl) Materiel.getMolette();
		
		this.setTitle("Metronome");
		this.getContentPane().setForeground(Color.YELLOW);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(450, 165);
		this.setResizable(false);
		this.getContentPane().setLayout(null);
		
		led1.setBounds(364, 11, 18, 14);
		led2.setBounds(364, 38, 18, 14);
		
		this.getContentPane().add(led2);
		this.getContentPane().add(led1);
		this.getContentPane().add(molette);
		
		//Creation du bouton START
		start = new JButton("START");
		start.setBounds(10, 84, 89, 23);
		this.getContentPane().add(start);
		
		//Creation du bouton STOP
		stop = new JButton("STOP");
		stop.setBounds(109, 84, 89, 23);
		this.getContentPane().add(stop);
		
		
		//Creation du bouton INC
		inc = new JButton("INC");
		inc.setBounds(226, 84, 89, 23);
		this.getContentPane().add(inc);
		
		//Creation du bouton DEC
		dec = new JButton("DEC");
		dec.setBounds(325, 84, 89, 23);
		this.getContentPane().add(dec);
		

		//Creation du label tempoText
		tempoText = new JLabel("Tempo : ");
		tempoText.setBounds(203, 11, 42, 23);
		this.getContentPane().add(tempoText);
		
		//Creation du label tempoValue
		tempoValue = new JLabel("40");
		tempoValue.setBounds(268, 11, 47, 23);
		this.getContentPane().add(tempoValue);
		
		JLabel lblMesure = new JLabel("Mesure");
		lblMesure.setBounds(308, 11, 46, 14);
		this.getContentPane().add(lblMesure);
		
		JLabel lblTemps = new JLabel("Temps");
		lblTemps.setBounds(308, 38, 46, 14);
		this.getContentPane().add(lblTemps);
		
		
		this.setVisible(true);
		
		
		//Ajout des listeners pour les composants
		start.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent arg0) {
				Materiel.getClavier().modifierEtatTouche(1, false);
				
			}
			
			@Override
			public void mousePressed(MouseEvent arg0) {
				Materiel.getClavier().modifierEtatTouche(1, true);
				
			}
			
			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});		
		
		stop.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent arg0) {
				Materiel.getClavier().modifierEtatTouche(2, false);
				
			}
			
			@Override
			public void mousePressed(MouseEvent arg0) {
				Materiel.getClavier().modifierEtatTouche(2, true);
				
			}
			
			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});		
		
		inc.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent arg0) {
				Materiel.getClavier().modifierEtatTouche(3, false);
				
			}
			
			@Override
			public void mousePressed(MouseEvent arg0) {
				Materiel.getClavier().modifierEtatTouche(3, true);
				
			}
			
			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});		
		
		dec.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent arg0) {
				Materiel.getClavier().modifierEtatTouche(4, false);
				
			}
			
			@Override
			public void mousePressed(MouseEvent arg0) {
				Materiel.getClavier().modifierEtatTouche(4, true);
				
			}
			
			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});		
	}

	@Override
	public void allumerLED(int numLED) {
		listeLed.get(numLED).allumer();
	}

	@Override
	public void eteindreLED(int numLED) {
		listeLed.get(numLED).eteindre();
	}

	@Override
	public void afficherTempo(int valeurTempo) {
		tempoValue.setText(valeurTempo+"");
	}

	@Override
	public MoletteImpl getMolette() {
		return molette;
	}

	public String getTempoValue() {
		return tempoValue.getText();
	}

	@Override
	public Integer getTempsParMesure() {
		return tempsParMesure;
	}

	@Override
	public void setTempsParMesure(int tempsParMesure) {
		// TODO Auto-generated method stub
	}

	@Override
	public void setEnableIncDec(boolean enable) {
		inc.setEnabled(enable);
		dec.setEnabled(enable);
		start.setEnabled(enable);
		stop.setEnabled(!enable);
	}
}
